home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / DllSys_Files / KOHONEN / SQKOH.C < prev   
Encoding:
C/C++ Source or Header  |  2002-03-08  |  2.2 KB  |  70 lines

  1. // Dynamic link library implementation of NeuroSolutions SquareKohonen component 
  2.  
  3. #include "NSDLL.h" 
  4.  
  5. /*************************************************************/
  6. /* Macros to access the PE layers and weights in matrix form */
  7.  
  8. #define in(i,j)        input[j+i*inCols]
  9. #define out(i,j)    output[j+i*outCols]
  10. #define W(i,j)        weights[j+i*inCount]
  11.  
  12. /******************************/
  13. /* Unsupervised weight update */
  14.  
  15. __declspec(dllexport) void performKohonen(
  16.     DLLData    *instance,        // Pointer to instance data (may be NULL)
  17.     NSFloat    *input,            // Pointer to input layer 
  18.     int     inRows,            // Number of rows of PEs in the input layer
  19.     int     inCols,            // Number of columns of PEs in the input layer
  20.     NSFloat    *output,         // Pointer to output layer
  21.     int        outRows,         // Number of rows of PEs in the output layer
  22.     int        outCols,         // Number of columns of PEs in the output layer
  23.     NSFloat    *weights,        // Pointer to fully connected weight matrix 
  24.     NSFloat    step,             // Learning rate
  25.     int        winningRow,        // Index of winning row 
  26.     int        winningCol,        // Index of winning column 
  27.     int        size             // Size of the neighborhood
  28.     )
  29. {    
  30.     int    i,j,k,
  31.         inCount = inRows*inCols,
  32.         startRow = winningRow - size,
  33.         stopRow = winningRow + size,
  34.         startCol = winningCol - size,
  35.         stopCol = winningCol + size;
  36.  
  37.     if (startRow < 0)
  38.         startRow = 0;
  39.     if (stopRow >= outRows)
  40.         stopRow = outRows-1;
  41.     if (startCol < 0)
  42.         startCol = 0;
  43.     if (stopCol >= outCols)
  44.         stopCol = outCols-1;
  45.     for (i=startRow; i<=stopRow; i++)
  46.         for (j=startCol; j<=stopCol; j++)
  47.             for (k=0; k<inCount; k++)
  48.                 W(j+i*outCols,k) += step*(input[k] - W(j+i*outCols,k));
  49. }
  50.  
  51. /******************************************/
  52. /* Management of instance data (OPTIONAL) */
  53. /*
  54. __declspec(dllexport) DLLData *allocKohonen(
  55.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  56.     int     inRows,        // Number of rows of PEs in the input layer
  57.     int     inCols,        // Number of columns of PEs in the input layer
  58.     int     outRows,    // Number of rows of PEs in the output layer
  59.     int     outCols        // Number of columns of PEs in the output layer
  60.     )
  61. {
  62.     DLLData *instance = allocDLLInstance(oldInstance);
  63.     return instance;
  64. }
  65.  
  66. __declspec(dllexport) void freeKohonen(DLLData *instance)
  67. {
  68.     freeDLLInstance(instance);
  69. }
  70. */